package edu.gatech.arktos;

import java.awt.List;
import java.net.URL;

import com.google.gdata.client.DocumentQuery;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;

public class Session {
	private SpreadsheetService service = null;
	
	public int login(String username, String password){
		try {
			service = new SpreadsheetService("TDD1");
			service.setUserCredentials(username, password);
		}
		catch(Exception ex){
			return 1;
		}
		
		//0 = no problems
		//1 = ERROR
		return 0;
	}

	public GradesDB getDBByName(String gradesDb) {
		if (service == null){
			return null;
		}
		try {
			URL metafeedUrl = new URL("http://spreadsheets.google.com/feeds/spreadsheets/private/full");
			DocumentQuery dq = new DocumentQuery(metafeedUrl);
			dq.setTitleQuery(gradesDb);
			dq.setTitleExact(true);
			
			SpreadsheetFeed feed = service.getFeed(dq, SpreadsheetFeed.class);
			java.util.List<SpreadsheetEntry> spreadsheets = feed.getEntries();
			
			GradesDB Gradebook = new GradesDB(spreadsheets.get(0), service);
			return Gradebook;
		} catch(Exception ex){
			//fuck
		}
		
		return null;
	}

	public int logout() {
		service = null;
		return 0;
	}

	public static void main(String[] args){
		Session aSession = new Session();
		aSession.login(Constants.USERNAME, Constants.PASSWORD);
		System.out.println("TESTING");
		aSession.getDBByName(Constants.GRADES_DB);
	}
}
